{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<table class=\"ee-notebook-buttons\" align=\"left\">\n",
        "    <td><a target=\"_blank\"  href=\"https://github.com/giswqs/earthengine-py-notebooks/tree/master/MachineLearning/confusion_matrix.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /> View source on GitHub</a></td>\n",
        "    <td><a target=\"_blank\"  href=\"https://nbviewer.jupyter.org/github/giswqs/earthengine-py-notebooks/blob/master/MachineLearning/confusion_matrix.ipynb\"><img width=26px src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Jupyter_logo.svg/883px-Jupyter_logo.svg.png\" />Notebook Viewer</a></td>\n",
        "    <td><a target=\"_blank\"  href=\"https://colab.research.google.com/github/giswqs/earthengine-py-notebooks/blob/master/MachineLearning/confusion_matrix.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /> Run in Google Colab</a></td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Install Earth Engine API and geemap\n",
        "Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://geemap.org). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for interacting with Earth Engine data layers, such as `Map.addLayer()`, `Map.setCenter()`, and `Map.centerObject()`.\n",
        "The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its [dependencies](https://github.com/giswqs/geemap#dependencies), including earthengine-api, folium, and ipyleaflet."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Installs geemap package\n",
        "import subprocess\n",
        "\n",
        "try:\n",
        "    import geemap\n",
        "except ImportError:\n",
        "    print('Installing geemap ...')\n",
        "    subprocess.check_call([\"python\", '-m', 'pip', 'install', 'geemap'])"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "import ee\n",
        "import geemap"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create an interactive map \n",
        "The default basemap is `Google Maps`. [Additional basemaps](https://github.com/giswqs/geemap/blob/master/geemap/basemaps.py) can be added using the `Map.add_basemap()` function. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "Map = geemap.Map(center=[40,-100], zoom=4)\n",
        "Map"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Add Earth Engine Python script "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Add Earth Engine dataset\n",
        "# Define a region of interest as a point.  Change the coordinates\n",
        "# to get a classification of any place where there is imagery.\n",
        "roi = ee.Geometry.Point(-122.3942, 37.7295)\n",
        "\n",
        "# Load Landsat 5 input imagery.\n",
        "landsat = ee.Image(ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')\n",
        "  # Filter to get only one year of images. \\\n",
        "  .filterDate('2011-01-01', '2011-12-31')\n",
        "  # Filter to get only images under the region of interest. \\\n",
        "  .filterBounds(roi)\n",
        "  # Sort by scene cloudiness, ascending. \\\n",
        "  .sort('CLOUD_COVER')\n",
        "  # Get the first (least cloudy) scene. \\\n",
        "  .first())\n",
        "\n",
        "# Compute cloud score.\n",
        "cloudScore = ee.Algorithms.Landsat.simpleCloudScore(landsat).select('cloud')\n",
        "\n",
        "# Mask the input for clouds.  Compute the min of the input mask to mask\n",
        "# pixels where any band is masked.  Combine that with the cloud mask.\n",
        "input = landsat.updateMask(landsat.mask().reduce('min').And(cloudScore.lte(50)))\n",
        "\n",
        "# Use MODIS land cover, IGBP classification, for training.\n",
        "modis = ee.Image('MODIS/051/MCD12Q1/2011_01_01') \\\n",
        "    .select('Land_Cover_Type_1')\n",
        "\n",
        "# Sample the input imagery to get a FeatureCollection of training data.\n",
        "training = input.addBands(modis).sample(**{\n",
        "  'numPixels': 5000,\n",
        "  'seed': 0\n",
        "})\n",
        "\n",
        "# Make a Random Forest classifier and train it.\n",
        "classifier = ee.Classifier.randomForest(10) \\\n",
        "    .train(training, 'Land_Cover_Type_1')\n",
        "\n",
        "# Classify the input imagery.\n",
        "classified = input.classify(classifier)\n",
        "\n",
        "# Get a confusion matrix representing resubstitution accuracy.\n",
        "trainAccuracy = classifier.confusionMatrix()\n",
        "print('Resubstitution error matrix: ', trainAccuracy)\n",
        "print('Training overall accuracy: ', trainAccuracy.accuracy())\n",
        "\n",
        "# Sample the input with a different random seed to get validation data.\n",
        "validation = input.addBands(modis).sample(**{\n",
        "  'numPixels': 5000,\n",
        "  'seed': 1\n",
        "  # Filter the result to get rid of any {} pixels.\n",
        "}).filter(ee.Filter.neq('B1', {}))\n",
        "\n",
        "# Classify the validation data.\n",
        "validated = validation.classify(classifier)\n",
        "\n",
        "# Get a confusion matrix representing expected accuracy.\n",
        "testAccuracy = validated.errorMatrix('Land_Cover_Type_1', 'classification')\n",
        "print('Validation error matrix: ', testAccuracy)\n",
        "print('Validation overall accuracy: ', testAccuracy.accuracy())\n",
        "\n",
        "# Define a palette for the IGBP classification.\n",
        "igbpPalette = [\n",
        "  'aec3d4', # water\n",
        "  '152106', '225129', '369b47', '30eb5b', '387242', # forest\n",
        "  '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40',  # shrub, grass\n",
        "  '111149', # wetlands\n",
        "  'cdb33b', # croplands\n",
        "  'cc0013', # urban\n",
        "  '33280d', # crop mosaic\n",
        "  'd7cdcc', # snow and ice\n",
        "  'f7e084', # barren\n",
        "  '6f6f6f'  # tundra\n",
        "]\n",
        "\n",
        "# Display the input and the classification.\n",
        "Map.centerObject(ee.FeatureCollection(roi), 10)\n",
        "Map.addLayer(input, {'bands': ['B3', 'B2', 'B1'], 'max': 0.4}, 'landsat')\n",
        "Map.addLayer(classified, {'palette': igbpPalette, 'min': 0, 'max': 17}, 'classification')\n",
        "\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Display Earth Engine data layers "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "Map.addLayerControl() # This line is not needed for ipyleaflet-based Map.\n",
        "Map"
      ],
      "outputs": [],
      "execution_count": null
    }
  ],
  "metadata": {
    "anaconda-cloud": {},
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.1"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}